package tsg.component;

import java.util.ArrayList;

import tsg.event.FocusListener;
import tsg.event.KeyListener;
import tsg.event.MouseListener;
import tsg.event.TouchListener;
import tsg.graphic.Color;
import tsg.graphic.Graphics;
import tsg.laf.ComponentUI;
import tsg.physics.Collision;
import tsg.shape.Rectangle;
import tsg.util.Dimension;
import tsg.util.Point;

public abstract class Component 
{
	public Component()
	{
		_position = new Point();
		_size = new Dimension();
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void addMouseListener(MouseListener listener)
	{
		if(_mouseListeners == null)
		{
			_mouseListeners = new ArrayList<MouseListener>();
		}
		
		_mouseListeners.add(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void removeMouseListener(MouseListener listener)
	{
		if(_mouseListeners == null)
		{
			_mouseListeners = new ArrayList<MouseListener>();
		}
		
		_mouseListeners.remove(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void addKeyListener(KeyListener listener)
	{
		if(_keyListeners == null)
		{
			_keyListeners = new ArrayList<KeyListener>();
		}
		
		_keyListeners.add(listener);
	}
	
	
	/**
	 * 
	 * @param listener
	 */
	public void removeKeyListener(KeyListener listener)
	{
		if(_keyListeners == null)
		{
			_keyListeners = new ArrayList<KeyListener>();
		}
		
		_keyListeners.remove(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void addFocusListener(FocusListener listener)
	{
		if(_focusListeners == null)
		{
			_focusListeners = new ArrayList<FocusListener>();
		}
		
		_focusListeners.add(listener);
	}
	
	
	/**
	 * 
	 * @param listener
	 */
	public void removeFocusListener(FocusListener listener)
	{
		if(_focusListeners == null)
		{
			_focusListeners = new ArrayList<FocusListener>();
		}
		
		_focusListeners.remove(listener);
	}
	
	
	/**
	 * 
	 * @param listener
	 */
	public void addGestureListener(TouchListener listener)
	{
		if(_gestureListeners == null)
		{
			_gestureListeners = new ArrayList<TouchListener>();
		}
		
		_gestureListeners.add(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void removeGestureListener(TouchListener listener)
	{
		if(_gestureListeners == null)
		{
			_gestureListeners = new ArrayList<TouchListener>();
		}
		
		_gestureListeners.remove(listener);
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean isEnabled()
	{
		return _enabled;
	}
	
	
	/**
	 * 
	 * @param enabled
	 */
	public void setEnabled(boolean enabled)
	{
		_enabled = enabled;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public boolean hasFocus()
	{
		return _hasFocus;
	}
	
	
	/**
	 * 
	 */
	public void requestFocus()
	{
		// TODO
	}
	
	/**
	 * 
	 * @return Dimension
	 */
	public Dimension getPreferredSize()
	{
		return _preferredSize;
	}
	
	
	/**
	 * 
	 * @param preferredSize
	 */
	public void setPreferredSize(Dimension preferredSize)
	{
		_preferredSize = preferredSize;
	}
	
	/**
	 * 
	 * @return Dimension
	 */
	public Dimension getMinimumSize()
	{
		return _minimumSize;
	}
	
	
	/**
	 * 
	 * @param minimumSize
	 */
	public void setMinimumSize(Dimension minimumSize)
	{
		_minimumSize = minimumSize;
	}
	
	/**
	 * 
	 * @return Dimension
	 */
	public Dimension getMaximumSize()
	{
		return _maximumSize;
	}
	
	
	/**
	 * 
	 * @param maximumSize
	 */
	public void setMaximumSize(Dimension maximumSize)
	{
		_maximumSize = maximumSize;
	}
	
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean contains(int x, int y)
	{
		return Collision.pointLiesInRectangle(new Point(x, y), new Rectangle(_position, _size));
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean isOpaque()
	{
		return _isOpaque;
	}
	
	/**
	 * 
	 * @param isOpaque
	 */
	public void setOpaque(boolean isOpaque)
	{
		_isOpaque = isOpaque;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public boolean isVisible()
	{
		return _isVisible;
	}
	
	/**
	 * 
	 * @param isVisible
	 */
	public void setVisible(boolean isVisible)
	{
		_isVisible = isVisible;
	}
	
	/**
	 * 
	 * @param g
	 */
	public void paint(Graphics g)
	{
		if(this.isOpaque() == true)
		{
			_componentUI.paint(g, this);
		}
	}
	
	
	/**
	 * 
	 * @return
	 */
	public Color getBackgroundColor()
	{
		return _backgroundColor;
	}
	
	
	/**
	 * 
	 * @param backgroundColor
	 */
	public void setBackgroundColor(Color backgroundColor)
	{
		_backgroundColor = backgroundColor;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public Color getForegroundColor()
	{
		return _foregroundColor;
	}
	
	
	/**
	 * 
	 * @param foregroundColor
	 */
	public void setForegroundColor(Color foregroundColor)
	{
		_foregroundColor = foregroundColor;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public Dimension getSize()
	{
		return _size;
	}
	
	
	/**
	 * 
	 * @param size
	 */
	public void setSize(Dimension size)
	{
		_size = size;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public Point getPosition()
	{
		return _position;
	}
	
	
	/**
	 * 
	 * @param position
	 */
	public void setPosition(Point position)
	{
		_position = position;
	}
	

	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public Component getComponentAt(int x, int y)
	{
		Component result = null;
		
		if(this.contains(x, y) == true)
		{
			result = this;
		}
		
		return result;
	}
	
	/**
	 * 
	 */
	public void triggerLayoutManager()
	{
		// DO NOTHING, We are not a container
	}
	
	/**
	 * 
	 * @param componentUI
	 */
	public void setUI(ComponentUI componentUI)
	{
		_componentUI = componentUI;
	}
	

	private Color 						_backgroundColor	= null;
	private Color						_foregroundColor	= null;
	private Point						_position			= null;
	private Dimension					_size				= null;
	private ArrayList<MouseListener> 	_mouseListeners 	= null;
	private ArrayList<KeyListener> 		_keyListeners 		= null;
	private ArrayList<TouchListener> 	_gestureListeners 	= null;
	private ArrayList<FocusListener>	_focusListeners		= null;
	private boolean						_enabled			= true;
	private boolean						_hasFocus			= false;
	private Dimension					_preferredSize		= null;
	private Dimension					_minimumSize		= null;
	private Dimension					_maximumSize		= null;
	private boolean						_isOpaque			= true;
	private boolean						_isVisible			= true;
	private ComponentUI					_componentUI		= null;
}








